package com.zaker.touchtv.modules.live;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.zaker.touchtv.BaseActivity;
import com.zaker.touchtv.R;
import com.zaker.touchtv.component.PlayPauseView;

import static com.zaker.touchtv.utils.Constants.LOG_TAG;

public class LiveActivity extends BaseActivity {

  private static final String PUSH_URL_KEY = "push_url_key";
  private static final String LIVE_ID_KEY = "live_id_key";

  private LiveMainLayout liveMainLayout;
  private ILivePresenter livePresenter;

  private MaterialDialog mProgressDialog;

  private String rtmpSite;

  public static void startLiveActivity(String livePk, String pushUrl, Context packageContext) {
    Intent intent = new Intent(packageContext, LiveActivity.class);
    intent.putExtra(PUSH_URL_KEY, pushUrl);
    intent.putExtra(LIVE_ID_KEY, livePk);
    packageContext.startActivity(intent);
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    liveMainLayout = LiveMainLayout.newInstance(getLayoutInflater(), null, false);
    setContentView(liveMainLayout);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    rtmpSite = getIntent().getStringExtra(PUSH_URL_KEY);
    String liveId = getIntent().getStringExtra(LIVE_ID_KEY);

    livePresenter = new LiveMainController(this, liveMainLayout, liveId);

    Log.i(LOG_TAG, "LiveActivity onCreate rtmpSite: " + rtmpSite);
  }

  @Override
  protected void onPause() {
    super.onPause();
    livePresenter.onPauseLive();
  }

  @Override
  protected void onResume() {
    super.onResume();
    livePresenter.onResumeLive();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    livePresenter.onStopLive();
    dismissProgressDialog();
  }

  void showProgressDialog() {
    dismissProgressDialog();
    mProgressDialog = new MaterialDialog.Builder(this)
        .progress(true, 0)
        .progressIndeterminateStyle(false)
        .title(R.string.test_live_tip_text)
        .content(R.string.launch_live_text)
        .cancelable(false)
        .canceledOnTouchOutside(false)
        .onAny(new MaterialDialog.SingleButtonCallback() {
          @Override
          public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
            Log.i(LOG_TAG, " DialogAction name: " + which.name());
            //dialog.setCancelable(false);
          }
        })
        .negativeText(R.string.cancel)
        .show();
  }

  void dismissProgressDialog() {
    if (mProgressDialog != null) {
      mProgressDialog.dismiss();
      mProgressDialog = null;
    }
  }

  public void onClickActionLive(View view) {

    if (!(view instanceof PlayPauseView)) {
      return;
    }
    final PlayPauseView playPauseView = (PlayPauseView) view;
    playPauseView.setAnimatorEndListener(new PlayPauseView.AnimatorEndListener() {
      @Override
      public void onAnimationEnd() {
        if (liveMainLayout != null) {
          liveMainLayout.ensureLiveStateTip();
        }
      }
    });

    if (playPauseView.getPlayPauseDrawable().isPlay()) {
      if (livePresenter.isStart()) {
        livePresenter.onResumeLive();
      } else {
        showProgressDialog();
        livePresenter.onStartLive(rtmpSite);
      }
    } else {
      livePresenter.onPauseLive();
    }

    //dismissProgressDialog();
    playPauseView.toggle();
  }

  public void onSwitchCamera(View view) {
    livePresenter.switchCamera();
  }
}
